package com.fsharemobile;

import java.util.List;

import com.fsharemobile.client.FClient;
import com.fsharemobile.client.File;
import com.fsharemobile.client.FileProxy;
import com.fsharemobile.client.UserProxy;
import com.fsharemobile.client.FileProxy.OnGetListCompleted;
import com.fsharemobile.client.UserProxy.OnLoginCompleted;
import com.fsharemobile.model.Share;
import com.fsharemobile.model.User;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
	TextView txtForgotPass;
	EditText edtEmail,edtPass;
	Button btnLogin;
	public static final boolean isLogin=false;
	String email,pass;
	User currentUser;
	
	private FClient fclient = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		Share share = Share.getInstance();
		this.fclient = share.getFclient();
		final UserProxy userProxy = fclient.getUserProxy();
		txtForgotPass = (TextView) findViewById(R.id.txt_forgot_pass);
		edtEmail = (EditText) findViewById(R.id.edt_email);
		edtPass = (EditText) findViewById(R.id.edt_password);
		btnLogin = (Button) findViewById(R.id.btn_login);
		edtEmail.setText("minhdta@fpt.net");
		
	
		if(txtForgotPass!=null){
			txtForgotPass.setMovementMethod(LinkMovementMethod.getInstance());
		}
		
		btnLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				email= edtEmail.getText().toString();
				edtPass.setText("1234567");
				pass= edtPass.getText().toString();
				try {

					userProxy.login(email, pass,
							new OnLoginCompleted() {
								@Override
								public void onLoginCompleted(int errCode) {
									System.out.println(errCode);
									System.out.println(fclient.getSessionid());

									//FileProxy fileProxy = fclient.getFileProxy();

									onLoginCompelted(errCode);

								}
							});

					/*
					 * user_api.getList(new OnGetListCompleted() {
					 * 
					 * @Override public void onGetListCompleted(String result) { // TODO
					 * Auto-generated method stub System.out.println(result); } });
					 */
				} catch (Exception ex) {
					System.out.println(ex.toString());
				} finally {

				}		
				
				
				
				
				/*email= edtEmail.getText().toString();
				edtPass.setText("1234567");
				pass= edtPass.getText().toString();
				
				
				if(pass.compareTo("1234567")==0){
					currentUser= new User();
					currentUser.setId("01");
					currentUser.setUserEmail(email);
					currentUser.setLevel("Vip");
					currentUser.setTotalPoints("50000");
					currentUser.setName("Đặng Thái Anh Minh");
					currentUser.setTraffic("50");
					currentUser.setTrafficUsed("10");
					//Bundle myData= new Bundle();
					//myData.putParcelable("user", currentUser);
*/					
					
				/*}else{
					Toast.makeText(LoginActivity.this, "Your password or email address was wrong, please check again!!! ", Toast.LENGTH_SHORT).show();
				}*/
			}
		});
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}
	
	public int  cong(){
		int  cong=10;
		return cong;
				
	}
	
	private void onLoginCompelted(int errorCode){
		if(errorCode==0){
			Intent myHome= new Intent(LoginActivity.this, com.fsharemobile.HomeActivity.class);
			myHome.putExtra("user",currentUser);
			startActivity(myHome);
			finish();
		}else{
			Toast.makeText(LoginActivity.this, "null casc", Toast.LENGTH_SHORT).show();
		}
		
	}

}
